package com.pz.art.controller;

import com.pz.art.service.UnitService;
import com.pz.art.domain.AssoMemberUnit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @params
 * @note 协会单位控制器
 * @date
 * @author lw
 */
@RestController
@RequestMapping("/association")
public class UnitController extends BaseController {

    @Autowired
    private UnitService unitService;

    /**
     * 获取单位会员列表
     */
    @PreAuthorize("@ss.hasPermi('association:associationlist')")
    @GetMapping("/associationlist")
    public TableDataInfo list(AssoMemberUnit assoMemberUnit)
    {
        startPage();
        List<AssoMemberUnit> list = unitService.selectAllMember(assoMemberUnit);
        return getDataTable(list);
    }

    @Log(title = "会员单位导入管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('association:importData')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<AssoMemberUnit> util = new ExcelUtil<AssoMemberUnit>(AssoMemberUnit.class);
        List<AssoMemberUnit> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = unitService.importUnit(userList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<AssoMemberUnit> util = new ExcelUtil<AssoMemberUnit>(AssoMemberUnit.class);
        util.importTemplateExcel(response, "会员单位数据");
    }

    @Log(title = "会员单位管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('association:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssoMemberUnit memberUnit)
    {
        try {
            List<AssoMemberUnit> list = unitService.selectAllMember(memberUnit);
            ExcelUtil<AssoMemberUnit> util = new ExcelUtil<AssoMemberUnit>(AssoMemberUnit.class);
            util.exportExcel(response, list, "用户数据");
        }catch (Throwable e){
            System.out.println(e.getMessage());
        }

    }

    //获取单个会员详情
    @PreAuthorize("@ss.hasPermi('association:unitdetail')")
    @GetMapping("/unitdetail/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(unitService.selectMemberById(id));
    }

    //修改单个会员单位信息
    @PreAuthorize("@ss.hasPermi('association:unitupdate')")
    @PutMapping("/unitupdate")
    public AjaxResult edit(@RequestBody AssoMemberUnit assoMemberUnit)
    {
        System.out.println(assoMemberUnit);
        return toAjax(unitService.updateMember(assoMemberUnit));
    }

    //注销单位
    @PreAuthorize("@ss.hasPermi('association:unitstatus')")
    @PutMapping("/unitstatus/{id}")
    public AjaxResult remove(@RequestBody AssoMemberUnit assoMemberUnit)
    {
        Integer id = assoMemberUnit.getId();
        Integer status = assoMemberUnit.getStatus();
        return toAjax(unitService.deleteMemberById(id,status));
    }
}
